package com.bicon.botu.account.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.account.entity.Role;
import com.bicon.botu.account.service.IRoleService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 角色增删改查接口类
 *
 * @author peizp
 * @date 2017/6/14 0012 下午 2:47
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService iRoleService;

    /**
     * 新增角色
     *
     * @param name 角色名
     * @param code 角色code
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult add(@RequestParam String name, @RequestParam String code, @RequestParam String type) {
        boolean result = iRoleService.add(name, code, type);
        if (result) {
            return sendResult200();
        }
        return sendResult500();
    }

    /**
     * 删除角色
     *
     * @param id   角色id
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/delete")
    public BaseResult delete(@RequestParam String id, @RequestParam String type) {
        BaseResult baseResult = iRoleService.delete(id, type);
        return baseResult;
    }

    /**
     * 修改角色
     *
     * @param id   角色id
     * @param name 角色名
     * @param code 角色code
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam String id, @RequestParam String name, @RequestParam String code, @RequestParam String type) {
        BaseResult baseResult = iRoleService.update(id, name, code, type);
        return baseResult;
    }

    /**
     * 分页查询角色
     *
     * @param pageNo   页码
     * @param pageSize 每页数量
     * @param type     账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseResult select(String pageNo, String pageSize, String type) {
        int pno = Integer.parseInt(pageNo);
        int pSize = Integer.parseInt(pageSize);
        Page<Role> page = iRoleService.selectRoleByPageNoAndPageSizeAndType(pno, pSize, type);
        return sendResult200(page);
    }

    /**
     * 查询所有角色
     *
     * @param type 账户类型：0：前台账户，1：后台账户
     * @return
     */
    @RequestMapping(value = "/selectAll")
    public BaseResult selectAll(@RequestParam String type) {
        return iRoleService.findAllForController(type);
    }

}
